<?php
    @header("Content-type:text/html;charset=utf-8");
    @include_once("./connect.php");

    if(!(isset($_POST["id"]) && isset($_POST["user"]) && isset($_POST["num"]))){
        $obj = array();
        $obj["status"] = false;
        $obj["msg"] = "请传入参数";
        $json = json_encode($obj);
        exit($json);
    }

    $id = $_POST["id"];
    $user = $_POST["user"];
    $num = $_POST["num"];
    
    $sql = "SELECT * FROM `indexgoods` where goodsId = '$id'";
    
    $result = mysqli_query($conn,$sql);
    
    $obj = array();
    
    if($result){
        $item = mysqli_fetch_assoc($result);
        if($item){

            $gName = $item["goodsName"];
            $gPrice = $item["goodsPrice"];
            $gImg = $item["goodsImg"];
            
            
            $searchOne = "select * from `shopcar` where user = '$user' and gid = '$id'";
            $result1 = mysqli_query($conn,$searchOne);
            
            if($result1){
                $item1 = mysqli_fetch_assoc($result1);

                if($item1){
                    $update = "update `shopcar` set num = num+$num where gid = '$id' and user = '$user'";
                    $result3 = mysqli_query($conn,$update);

                    if($result3){
                        $rows = mysqli_affected_rows($conn);

                        if($rows>0){
                            $obj["status"] = true;
                            $obj["msg"] = "更新成功";
                        }else{
                            $obj["status"] = true;
                            $obj["msg"] = "更新失败";
                        }
                    }else{
                        $obj["status"] = false;
                        $obj["msg"] = "sql语句err";
                        $obj["sql"] = $update;
                    }
                }else{
                    $insert = "INSERT INTO `shopcar` (user,gid,gName,gImg,gPrice,num) VALUES ('$user','$id','$gName','$gImg','$gPrice','$num')";

                    $result2 = mysqli_query($conn,$insert);

                    if($result2){
                        $rows = mysqli_affected_rows($conn);

                        if($rows>0){
                            $obj["status"] = true;
                            $obj["msg"] = "新增成功";
                        }else{
                            $obj["status"] = true;
                            $obj["msg"] = "新增失败";
                        }
                    }else{
                        $obj["status"] = false;
                        $obj["msg"] = "sql语句err";
                        $obj["sql"] = $insert;
                    }
                }
            }else{
                $obj["status"] = false;
                $obj["msg"] = "sql语句err";
                $obj["sql"] = $searchOne;
            }
        }
    }else{
        $obj["status"] = false;
        $obj["msg"] = "sql语句err";
        $obj["sql"] = $sql;
    }
    echo json_encode($obj);



?>